﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Collections;
using System.Collections.ObjectModel;
using Windows8Accelerator.Core.Xaml.Serialization;

namespace Windows8Accelerator.Core.Xaml.Storage
{
    public enum ObjectPersisterFormat
    {
        //Xml,
        Binary,
        SimpleXml,
    }

    public static class ObjectPersister
    {
        public static async Task<bool> SaveAsync(string name, StorageLocation location, object obj, ObjectPersisterFormat format = ObjectPersisterFormat.Binary)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            try
            {
                using (var stream = await StorageHelperBase.Instance.OpenFileWriteAsync(name, location, true))
                {
                    switch (format)
                    {
                        //case ObjectPersisterFormat.Xml:
                            //XmlSerializer.Serialize(stream, obj);
                        //    break;
                        case ObjectPersisterFormat.Binary:
                            BinarySerializer.Serialize(stream, obj);
                            break;
                        case ObjectPersisterFormat.SimpleXml:
                            SimpleXmlSerializer.Serialize(stream, obj);
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                    await stream.FlushAsync();
                }
                return true;
            }
            catch { }
            return false;
        }

        public static async Task<bool> DeleteAsync(string name, StorageLocation location)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            try{
                await StorageHelperBase.Instance.DeleteFilesAsync(name, location);
                return true;
            }
            catch 
            {
            }
            return false;
        }

        public static async Task<T> LoadAsync<T>(string name, StorageLocation location, ObjectPersisterFormat format = ObjectPersisterFormat.Binary)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            try
            {
                using (var stream = await StorageHelperBase.Instance.OpenFileReadAsync(name, location))
                {
                    switch (format)
                    {
                        //case ObjectPersisterFormat.Xml:
                            //return XmlSerializer.Deserialize<T>(stream.AsStreamForRead());
                        case ObjectPersisterFormat.SimpleXml:
                            return SimpleXmlSerializer.Deserialize<T>(stream);
                        case ObjectPersisterFormat.Binary:
                            return (T)BinarySerializer.Deserialize(stream);
                        default:
                            throw new NotImplementedException();
                    }
                }
            }
            catch (FileNotFoundException)
            {
                return default(T);
            }
        }


      
    }

}
